﻿-- MODIFICATION OF THIS CODE IS ONLY PERMITTED WITH WRITTEN CONSENT FROM THE ORIGINAL AUTHOR.
-- IF YOU WOULD LIKE TO MAKE A MODIFICATION PLEASE REQUEST IT IN WRITING BY SENDING AN E-MAIL TO
-- ORIGINAL AUTHOR AT: ootu@thecustomzguide.com

local ARENAS_ONLY 	= false
local status		= 2;


-- TEXTING COLORING FEATURES vvv------------------------------
local color_style 	= 	{
				red  	= {r = 0.875875, 	g = 0.475475,		b = 0.475475	},
				yellow 	= {r = 1.000000,	g = 1.000000, 		b = 0.250250	},
				blue 	= {r = 0.475475,	g = 0.475475, 		b = 0.875875	},
				green 	= {r = 0.475475, 	g = 0.875875,		b = 0.475475	},
				white	= {r = 1.000000, 	g = 1.000000, 		b = 1.000000	},
				help  	= {r = 0.825825, 	g = 0.825825, 		b = 0.475475	}
				};

function Hex(r, g, b)
	if type(r) == "table" then
		if r.r then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	return string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
end

local function PrintLine(txt)
	DEFAULT_CHAT_FRAME:AddMessage(tostring(txt))
end

-- example usage: PrintLine(Hex(color_style.help).."message");
-- TEXTING COLORING FEATURES ^^^------------------------------

failMessage 	= "%s FAILED - (%s)"
spells = {
	["Blade Flurry"]			= { msg = "{skull} Blade Flurry Used {skull}" },
	["Adrenaline Rush"]			= { msg = "{skull} Adrenaline Rush Used {skull}" },
	["Blind"]				= { msg = "{skull} Blind used on %s {skull}" },
	["Evasion"]				= { msg = "{skull} Evasion Used {skull}" },
	["Sap"]					= { msg = "{skull} 'Sapping' %s {skull}" },
	["Kick"]				= { msg = "{skull} Kicked %s in the teeth {skull}" },
	["Distract"]				= { msg = "{skull} Distract used {skull}" },
	["Tricks of the Trade"]			= { msg = "{skull} Tricks of the Trade applied to %s {skull}" },
	
	["Challenging Roar"]			= { msg = "{diamond} Challenging Roar used, Healers keep me Healed {diamond}" },
	["Innervate"]				= { msg = "{diamond} Inntervate on Cooldown, applied to %s {diamond}" },
	["Rebirth"]				= { msg = "{skull} Imcoming Battle Rezz to %s. Battle Rez now on CD {skull}" },
	["Revive"]				= { msg = "{diamond} Imcoming 'Revive' to %s one moment Please. {diamond}" },
	["Tranquility"]				= { msg = "{diamond} Tranquility has been used Raidwide Healing!!! {diamond}" },
	
	["Misdirection"]			= { msg = "{skull} Misdirection applied to %s {skull}" },
	["Deterrence"]				= { msg = "{skull} Deterrence has been used. {skull}" },
	["Wyvern Sting"]			= { msg = "{skull} Putting %s to sleep for 30 seconds {skull}" },
	["Rapid Fire"]				= { msg = "{skull} Rapid Fire has been used. Now on 5 min CD. {skull}" },

	["Shackle Undead"]			= { msg = "{square} Shackle Undead used. {square}" },
	["Resurrection"]			= { msg = "{square} Incoming 'Resurrection to %s {square}" },
	["Divine Hymn"]				= { msg = "{square} Divine Hymn now on Cool-Down. {square}" },
	["Divine Hope"]				= { msg = "{square} Divine Hope now on Cool-Down. {square}" },
	["Power Infusion"]			= { msg = "{square} Power Infusion applied to %s. {square}" },
	["Pain Suppression"]			= { msg = "{square} Pain Suppression applied to %s. {square}" },
	["Guardian Spirit"]			= { msg = "{square} Guardian Spirit applied to %s. {square}" },


	["Lay on Hands"]			= { msg = "{x}{x} Lay of Hands used on %s {x}{x}" },
	["Divine Intervention"]			= { msg = "{x}{x} Divine Intervention applied to %s {x}{x}" },
	["Redemption"]				= { msg = "{x}{x} Redemption applied to %s {x}{x}" },
	["Hand of Protection"]			= { msg = "{x}{x} Hand of Protection applied to %s {x}{x}" },
	["Divine Plea"]     			= { msg = "{x}{x} Divine Plea used. Temporarily healing for 50 percent! {x}{x}" },
	["Divine Intervention"] 		= { msg = "{x}{x} Divine Intervention applied to %s {x}{x}" },
	["Hand of Freedom"] 			= { msg = "{x}{x} Hand of Freedom used on %s {x}{x}" },
	["Hand of Salvation"]   		= { msg = "{x}{x} Hand of Salvation used on %s {x}{x}" },
	["Hand of Sacrifice"]   		= { msg = "{x}{x} Hand of Sacrifice used on %s {x}{x}" },
	["Divine Shield"]       		= { msg = "{star}{star} SUPER MARIO TIME! {star}{star}" },
	["Divine Sacrifise"]    		= { msg = "{star}{star} Raidwide Shield Wall active. {star}{star}" },

	
	["Bloodlust"]				= { msg = "{skull} 'Bloodlust' is now on 10 min Cooldown {skull}" },
	["Heroism"]				= { msg = "{skull} 'Heroism' is now on 10 min Cooldown {skull}" },
	["Mana Tide"]				= { msg = "{skull} Mana Tide has been used {skull}" },
	["Ancestral Spirit"]			= { msg = "{skull} Incoming 'Ancestral Spirit' to %s {skull}" },

	["Banish"]				= { msg = "{star} Banishing %s {star}" },
	["Ritual of Summoning"]			= { msg = "{star} Incoming Summoning to %s {star}" },
	["Ritual of Souls"]			= { msg = "{star} Ritual of Souls, applied to %s {star}" },
	["Soulstone Resurrection"]		= { msg = "{star} Soulstone Rezz being set to %s {star}" },

	["Last Stand"]				= { msg = "{skull} Last Stand has been used! {skull}" },
	["Shield Wall"]				= { msg = "{skull} Shield Wall has been used! {skull}" },
	["Recklessness"]			= { msg = "{skull} Recklessness has been used! {skull}" },
	["Shattering Throw"]			= { msg = "{skull} Shattering Throw now on 5 min cool-down used on %s! {skull}" },

	["Drums of Battle"]			= { msg = "{x} Drums of Battle - USED! {x}" },
	["Drums of War"]			= { msg = "{x} Drums of War - USED! {x}" },
	["Drums of Restoration"]		= { msg = "{x} Drums of Restoration - USED! {x}" },
	["Defibrillate"] 			= { msg = "{x} Attempting Engineer Rezz on %s! {x}" },

	["Army of the Dead"]			= { msg = "{x} Army of the Dead is now on Cooldown! {x}" }
}

if (GetLocale() == "zhCN") then
failMessage 	= "%s 失败 - (%s)"
spells = {
	--Rouge
	["剑刃乱舞"]			= { msg = "{骷髅} 剑刃乱舞 {骷髅}" },
	["冲动"]				= { msg = "{骷髅} 冲动 {骷髅}" },
	["致盲"]				= { msg = "{骷髅} 致盲 %s {骷髅}" },
	["闪避"]				= { msg = "{骷髅} 闪避 {骷髅}" },
	["闷棍"]				= { msg = "{骷髅} 闷棍 %s {骷髅}" },
	["脚踢"]				= { msg = "{骷髅} 脚踢 %s {骷髅}" },
	["扰乱"]				= { msg = "{骷髅} 扰乱 {骷髅}" },
	["嫁祸诀窍"]			= { msg = "{骷髅} 嫁祸诀窍 %s {骷髅}" },
	
	--Druid
	["挑战咆哮"]			= { msg = "{方块} 我已使用挑战咆哮,请治疗注意 {方块}" },
	["激活"]				= { msg = "{方块} 激活 %s {方块}" },
	["复生"]				= { msg = "{骷髅} 战复 %s {骷髅}" },
	["起死回生"]			= { msg = "复活 %s " },
	["宁静"]				= { msg = "{方块} 宁静 {方块}" },
	
	--Hunter
	["误导"]				= { msg = "{骷髅} 误导 %s {骷髅}" },
	["威慑"]				= { msg = "{骷髅} 威慑 {骷髅}" },
	["翼龙钉刺"]			= { msg = "{骷髅} 翼龙钉刺 %s {骷髅}" },
	["急速射击"]			= { msg = "{骷髅} 急速射击 {骷髅}" },

	--Priest
	["束缚亡灵"]			= { msg = "{方块} 束缚亡灵 %s {方块}" },
	["复活术"]				= { msg = "{方块} 复活 %s {方块}" },
	["神圣赞美诗"]			= { msg = "{方块} 神圣赞美诗 {方块}" },
	["Divine Hope"]			= { msg = "{方块} Divine Hope now on Cool-Down. {方块}" },
	["能量灌注"]			= { msg = "{方块} 能量灌注 %s {方块}" },
	["痛苦压制"]			= { msg = "{方块} 痛苦压制 %s {方块}" },
	["守护之魂"]			= { msg = "{方块} 守护之魂 %s {方块}" },

	--Paladin
	["圣疗术"]				= { msg = "{十字}{十字} 圣疗 %s {十字}{十字}" },
	["神圣干涉"]			= { msg = "{十字}{十字} 干涉 %s {十字}{十字}" },
	["救赎"]				= { msg = "{十字}{十字} 复活 %s {十字}{十字}" },
	["保护之手"]			= { msg = "{十字}{十字} 保护之手 %s {十字}{十字}" },
	["Divine Plea"]     	= { msg = "{十字}{十字} Divine Plea used. Temporarily healing for 50 percent! {十字}{十字}" },
	["自由之手"] 			= { msg = "{十字}{十字} 自由之手 %s {十字}{十字}" },
	["拯救之手"]   			= { msg = "{十字}{十字} 拯救之手 %s {十字}{十字}" },
	["牺牲之手"]   			= { msg = "{十字}{十字} 牺牲之手 %s {十字}{十字}" },
	["圣盾术"]       		= { msg = "{星形}{星形} 我已无敌,感觉良好,请祖国人民放心 {星形}{星形}" },
	["Divine Sacrifise"]   	= { msg = "{星形}{星形} Raidwide Shield Wall active. {星形}{星形}" },

	--Shaman
	["嗜血"]				= { msg = "{骷髅} 嗜血 {骷髅}" },
	["英勇"]				= { msg = "{骷髅} 英勇 {骷髅}" },
	["法力之潮图腾"]		= { msg = "{骷髅} 法力之潮图腾 {骷髅}" },
	["先祖之魂"]			= { msg = "{骷髅} 复活 %s {骷髅}" },

	--Warlock
	["放逐术"]				= { msg = "{星形} 放逐 %s {星形}" },
	["召唤仪式"]			= { msg = "{星形} 召唤仪式 {星形}" },
	["灵魂仪式"]			= { msg = "{星形} 灵魂仪式 {星形}" },
	["灵魂石复活"]			= { msg = "{星形} 灵魂石已绑 %s {星形}" },

	--Warrior
	["破釜沉舟"]			= { msg = "{骷髅} 破釜沉舟 {骷髅}" },
	["盾墙"]				= { msg = "{骷髅} 盾墙 {骷髅}" },
	["鲁莽"]				= { msg = "{骷髅} 鲁莽 {骷髅}" },
	["碎裂投掷"]			= { msg = "{骷髅} 碎裂投掷 %s {骷髅}" },

	--Specialty Items
	["战斗之鼓"]			= { msg = "{十字} 战斗之鼓 {十字}" },
	["战争之鼓"]			= { msg = "{十字} 战争之鼓 {十字}" },
	["恢复之鼓"]			= { msg = "{十字} 恢复之鼓 {十字}" },
	["电击"] 				= { msg = "{十字} 电击 %s {十字}" },

	--Death Knight
	["亡者大军"]			= { msg = "{十字} 亡者大军 {十字}" }
}

elseif (GetLocale() == "zhTW") then
failMessage 	= "%s 失敗 - (%s)"
spells = {
	--Rouge
	["劍刃亂舞"]			= { msg = "{頭顱} 劍刃亂舞 {頭顱}" },
	["能量刺激"]				= { msg = "{頭顱} 能量刺激 {頭顱}" },
	["致盲"]				= { msg = "{頭顱} 致盲 %s {頭顱}" },
	["閃避"]				= { msg = "{頭顱} 閃避! {頭顱}" },
	["悶棍"]				= { msg = "{頭顱} 悶棍 %s {頭顱}" },
	["腳踢"]				= { msg = "{頭顱} 腳踢 %s {頭顱}" },
	["擾亂"]				= { msg = "{頭顱} 擾亂 {頭顱}" },
	["偷天換日"]			= { msg = "{頭顱} 偷天換日 %s {頭顱}" },
	
	--Druid
	["挑戰咆哮"]			= { msg = "{方形} 已使用挑戰咆哮 {方形}" },
	["啟動"]				= { msg = "{方形} 啟動 %s {方形}" },
	["復生"]				= { msg = "{頭顱} 復生 %s {頭顱}" },
	["復活"]				= { msg = "復活 %s " },
	["寧靜"]				= { msg = "{方形} 寧靜 {方形}" },
	
	--Hunter
	["誤導"]				= { msg = "{頭顱} 誤導 %s {頭顱}" },
	["威懾"]				= { msg = "{頭顱} 威懾 {頭顱}" },
	["翼龍釘刺"]			= { msg = "{頭顱} 翼龍釘刺 %s {頭顱}" },
	["急速射擊"]			= { msg = "{頭顱} 急速射擊 {頭顱}" },

	--Priest
	["束縛不死生物"]		= { msg = "{方形} 束縛不死生物 %s {方形}" },
	["復活術"]				= { msg = "{方形} 復活 %s {方形}" },
	["神聖禮頌"]			= { msg = "{方形} 神聖禮頌 {方形}" },
	["Divine Hope"]			= { msg = "{方形} Divine Hope now on Cool-Down. {方形}" },
	["注入能量"]			= { msg = "{方形} 注入能量 %s {方形}" },
	["痛苦鎮壓"]			= { msg = "{方形} 痛苦鎮壓 %s {方形}" },
	["守護聖靈"]			= { msg = "{方形} 守護聖靈 %s {方形}" },

	--Paladin
	["聖療術"]				= { msg = "{十字}{十字} 聖療術 %s {十字}{十字}" },
	["神聖干涉"]			= { msg = "{十字}{十字} 神聖干涉 %s {十字}{十字}" },
	["救贖"]				= { msg = "{十字}{十字} 救贖 %s {十字}{十字}" },
	["保護聖禦"]			= { msg = "{十字}{十字} 保護聖禦 %s {十字}{十字}" },
	["Divine Plea"]     	= { msg = "{十字}{十字} Divine Plea used. Temporarily healing for 50 percent! {十字}{十字}" },
	["自由聖禦"] 			= { msg = "{十字}{十字} 自由聖禦 %s {十字}{十字}" },
	["拯救聖禦"]   			= { msg = "{十字}{十字} 拯救聖禦 %s {十字}{十字}" },
	["犧牲聖禦"]   			= { msg = "{十字}{十字} 犧牲聖禦 %s {十字}{十字}" },
	["聖盾術"]       		= { msg = "{星星}{星星} 聖盾術 {星星}{星星}" },
	["Divine Sacrifise"]   	= { msg = "{星星}{星星} Raidwide Shield Wall active. {星星}{星星}" },

	--Shaman
	["嗜血術"]				= { msg = "{頭顱} 嗜血術 {頭顱}" },
	["英勇氣概"]			= { msg = "{頭顱} 英勇氣概 {頭顱}" },
	["法力之潮圖騰"]		= { msg = "{頭顱} 法力之潮圖騰 {頭顱}" },
	["先祖之魂"]			= { msg = "{頭顱} 先祖之魂 %s {頭顱}" },

	--Warlock
	["放逐術"]				= { msg = "{星星} 放逐術 %s {星星}" },
	["召喚儀式"]			= { msg = "{星星} 召喚儀式 {星星}" },
	["靈魂儀式"]			= { msg = "{星星} 靈魂儀式 {星星}" },
	["靈魂石復活"]			= { msg = "{星星} 靈魂石復活 %s {星星}" },

	--Warrior
	["破釜沉舟"]			= { msg = "{頭顱} 破釜沉舟 {頭顱}" },
	["盾牆"]				= { msg = "{頭顱} 盾牆 {頭顱}" },
	["魯莽"]				= { msg = "{頭顱} 魯莽 {頭顱}" },
	["碎甲投擲"]			= { msg = "{頭顱} 碎甲投擲 %s {頭顱}" },

	--Specialty Items
	["戰鬥之鼓"]			= { msg = "{十字} 戰鬥之鼓 {十字}" },
	["戰爭之鼓"]			= { msg = "{十字} 戰爭之鼓 {十字}" },
	["恢復之鼓"]			= { msg = "{十字} 恢復之鼓 {十字}" },
	["電擊"] 				= { msg = "{十字} 電擊 %s {十字}" },

	--Death Knight
	["亡靈大軍"]			= { msg = "{十字} 亡靈大軍 {十字}" }
};
end

function iAnnounce_Usage()
	UpdateAddOnMemoryUsage()
	return floor(GetAddOnMemoryUsage('iAnnounce'));
end

function iAnnounce_OnLoad()
  	this:RegisterEvent("ADDON_LOADED");
end

function iAnnounce_OnEvent( event, ... )
  	if (event == "ADDON_LOADED") and (arg1 == "iAnnounce") then
  		PrintLine(Hex(color_style.red).."iAnnounce is now active, Memory Usage: "..iAnnounce_Usage().." Kb");
  	end
end

local cast = CreateFrame("Frame")
cast:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
cast:SetScript("OnEvent", function(self, event, ...)

	if( event == "COMBAT_LOG_EVENT_UNFILTERED" and select(2, ...) == "SPELL_MISSED" ) then
		local timestamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellID, spellName, spellSchool, missType = ...
		if( bit.band(sourceFlags, COMBATLOG_OBJECT_AFFILIATION_MINE) == COMBATLOG_OBJECT_AFFILIATION_MINE and spells[spellName] ) then
			
			if GetNumRaidMembers() > 0  then
				local spell = spells[spellName]
				SendChatMessage(string.format(spell.fail, spellName, string.upper(getglobal("ACTION_SPELL_MISSED_" .. missType))), "RAID")
			end
			if GetNumPartyMembers() > 0 and GetNumRaidMembers() == 0 then
				local spell = spells[spellName]
				SendChatMessage(string.format(spell.fail, spellName, string.upper(getglobal("ACTION_SPELL_MISSED_" .. missType))), "PARTY")
			end
		end
	

	elseif( event == "COMBAT_LOG_EVENT_UNFILTERED" and select(2, ...) == "SPELL_CAST_SUCCESS" ) then
		local timestamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellID, spellName, spellSchool, missType = ...
		if( bit.band(sourceFlags, COMBATLOG_OBJECT_AFFILIATION_MINE) == COMBATLOG_OBJECT_AFFILIATION_MINE and spells[spellName] ) then
			
			if GetNumRaidMembers() > 0  then
				local spell = spells[spellName]
				SendChatMessage(string.format(spell.msg, destName), "RAID")
			end
			if GetNumPartyMembers() > 0 and GetNumRaidMembers() == 0 then
				local spell = spells[spellName]
				SendChatMessage(string.format(spell.msg, destName), "PARTY")
			end
		end
	end
end)

-- Setup "defaults" quickly
for name, data in pairs(spells) do
	if( data.arenas == nil ) then
		data.arenas = ARENAS_ONLY
	end

	if( data.fail == nil ) then
		data.fail = failMessage
	end
end